package com.madhouse.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.madhouse.model.Student;
import com.madhouse.service.IStudentService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Controller
public class MyController {
	@Autowired
	private IStudentService studentService;
	
	@ApiOperation(
			value="/student/queryAll.action",
			httpMethod="GET",
			response=String.class,
			notes="to queryAll student")
	@RequestMapping(value="/student/queryAll.action", method=RequestMethod.GET)
	public String queryAll(Map<String,Object> map){
		List<Student> list = studentService.queryAll();
		map.put("list", list);
		return "listAll";
	}
	@ApiOperation(
			value="/student/deleteStudentById.action",
			httpMethod="GET",
			response=String.class,
			notes="to delete student by id")
	@RequestMapping(value="/student/deleteStudentById.action",method=RequestMethod.GET)
	public String deleteStudentById(@ApiParam(value="the primary key of student")Integer id){
		studentService.deleteStudentById(id);
		return "redirect:/student/queryAll.action";
	}
	@ApiOperation(
			value="/student/toUpdate.action",
			httpMethod="GET",
			response=String.class,
			notes="turn to update jsp file")
	@RequestMapping(value="/student/toUpdate.action",method=RequestMethod.GET)
	public String toUpdate(@ApiParam(value="the primary key of student")Integer id,Model model){
		Student student = studentService.queryStudentById(id);
		model.addAttribute("student",student);
		return "toUpdate";
	}
	@ApiOperation(
			value="/student/updateStudentById.action",
			httpMethod="POST",
			response=String.class,
			notes="update student infomation")
	@RequestMapping(value="/student/updateStudentById.action",method=RequestMethod.POST)
	public String updateStudentById(@ApiParam(value="student info,may not be null or enpty")Student student){
		if(student == null || student.getId() ==null){
			return "redirect:/student/queryAll.action";
		}
		studentService.updateStudentById(student);
		return "redirect:/student/queryAll.action";
	}
	@ApiOperation(
			value="/student/toAdd.action",
			httpMethod="GET",
			response=String.class,
			notes="turn to toAdd jsp file")
	@RequestMapping(value="/student/toAdd.action",method=RequestMethod.GET)
	public String toaddStudent(){
		return "toAdd";
	}
	@ApiOperation(
			value="/student/addStudent.action",
			httpMethod="POST",
			response=String.class,
			notes="to add student")
	@RequestMapping(value="/student/addStudent.action",method=RequestMethod.POST)
	public String addStudent(Student student){
		if(student == null || student.getId() == null){
			return "redirect:/student/queryAll.action";
		}
		studentService.addStudent(student);
		return "redirect:/student/queryAll.action";
	}
}
